<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Buyer\Controller;
use Think\Controller;
use User\Api\ProviderApi;
use Think\Model;

class LoginController extends Controller {
    public function index(){
        if(session('userid')){
            $title="商家首页";
            $this->title=$title;

            $res = M('Purchase_list')->where(array("status"=>2))->find();
            $map['batch'] = $res['batch'];
            $map['status'] = array('lt',2);
            $list = M('Purchase')->where($map)->select();
            foreach($list as $key => $val){
                if(get_provider_permission($val['pid'])){
                    $this->count = 1;
                }
            }
            $this->display();
        }else{
            $this->redirect('Login/login');
        }
    }
    public function login(){
        if($_SESSION['LOGIN']['name'] ){
            $this->assign('name',$_SESSION['LOGIN']['name']);
            $this->assign('password',$_SESSION['LOGIN']['password']);
        }
        $this->display();
    }
    public function gologin(){
        $username = $_POST['name'];
        $password = $_POST['password'];
        $map['name'] = $username ;
        $map['pwd'] = md5($password) ;
        $User = new ProviderApi;
        $uid = $User->login($username, $password);
        if($uid > 0){ //UC登录成功
            /* 登录用户 */
            session('userid',$uid);
            session('username',$username);
            session('userid',$uid,'LOGIN');
            session('username',$username,'LOGIN');
            $this->success('登录成功！', U('Login/index'),0);

        } else { //登录失败
            switch($uid) {
                case -1: $error = '用户不存在或被禁用！'; break; //系统级别禁用
                case -2: $error = '密码错误！'; break;
                default: $error = '未知错误！'; break; // 0-接口参数错误（调试阶段使用）
            }
            $this->error($error);
        }
    }
    //今日待采购
    public function todaypurchase(){
        $map['status'] =  1010;
//        $map['createdUser'] = session('userid') ;
        $purchase = M('Login')->where($map)->select();
        $title = "今日待采购" ;
        $this->title=$title;
        $this->assign('_list',$purchase);
        $this->display();
    }
    public function createpurchase(){
        if ( empty($this->purchase) ) {
            $this->assign('purchase',array('proName'=>null,'id'=>null,'spec'=>null));//排除notice信息
        }
        if(session('userid')){
            $title = "输入采购商品" ;
            $this->title=$title;
            $this->display('productpurchase');
        }else{
            $this->redirect('Login/login');
        }

    }
    public function editpurchase(){
        $title = "输入采购商品" ;
        $this->title=$title;
        $result = M('purchase')->find( (int)$_GET['id'] );
        $this->assign('purchase',$result);
        $this->display('productpurchase');
    }
    public function editpurchaselist(){
        $title = "输入采购商品" ;
        $this->title=$title;
        $result = M('purchaselist')->find( (int)$_GET['id'] );
        $this->assign('purchase',$result);
        $this->display('purchase');
    }
    //提交采购单
    public function submitpurchase(){
        //status：{1:待采购,2：已采购,3：已提交财务,4:已完成}

        $map['status'] =  1012;
        $map['updatedUser'] = session('userid') ;
        $purchase = M('Purchaselist')->where($map)->select();
        $AllCount = M('Purchaselist')->where($map)->count();

        $inPurchase['status'] = 1011 ;
        $inPurchase['updatedUser'] = session('userid') ;
        $inPurchaseCount = M('Purchaselist')->where($map)->count();

        $title = "提交采购单" ;
        $this->title=$title;
        $this->assign('_count',$AllCount);
        $this->assign('_inPurCount',$inPurchaseCount);

        $this->assign('_list',$purchase);
        $this->display();
    }

    //提交采购单  单一商品
    public function updatePurchase(){
        //提交表单后，给提示，是否继续录入和返回首页
        $purchase = M('purchaselist') ;
        $data1 = $purchase->create();
        if($data1){
            $data1['status'] = 1012 ;

            $data1['purchaser'] =  session('username') ;
            if(empty($data1['id'])){
                $data1['createdAt'] = NOW_TIME ;
                $data1['createdUser'] = session('userid') ;
                $data1['updatedAt'] = NOW_TIME ;
                $data1['updatedUser'] = session('userid') ;
                $re = $purchase->add($data1);
            }else{
                $data1['updatedAt'] = NOW_TIME ;
                $data1['updatedUser'] = session('userid') ;
                $re = $purchase->save($data1);
            }
            if($re===false && $re == false){
                $this->error('操作失败'.$purchase->getError());
            } else{
                $Model = new Model();
                $Model->execute("UPDATE gr_purchaselist SET cgje = grossWeight/number*price+2.5*number+boxfee WHERE id = ".$data1['id']);

                $Model->execute("UPDATE gr_purchaselist SET dwzjz = grossWeight/number-boxheavy*number*0.95 WHERE id = ".$data1['id']);

                $Model->execute("UPDATE gr_purchaselist SET dwcb = FORMAT(cgje/dwzjz,2) WHERE id = ".$data1['id']);

                $this->success('操作成功!',U('submitpurchase'));

            }
        }else{
            $this->error('操作失败'.$purchase->getError());
        }
    }
    public function submitAllPurchase(){
        //修改所有当前页的status=3，表示已提交至财务。财务需要将其修改为4
        $arriveTime = I('arriveTime');
        $map['status'] =  1012;
        $map['purchaser'] =  session('username') ;
        $map['updatedUser'] = session('userid') ;
        $data['status'] = 1013 ;
        $data['arriveTime'] = $arriveTime ;
        $data['updatedAt'] = NOW_TIME ;
        $data['updatedUser'] = session('userid') ;
        $purchase = M('purchaselist')->where($map)->save($data);
        if($purchase===false){
            $this->error('操作失败'.$purchase->getError());
        } else{
            $this->success('操作成功!',U('index'),0);
        }
    }

    public function historylist(){
        $map['uid']=session('userid');
        $map['status']=1;
        $goods=M('provider_report')->where($map)->select();
        $this->assign('_goods',$goods);
        $this->display();
    }

}
